<template>
  <j-modal
    :title="title"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
    okText="保存">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="expressCompany" label="物流公司">
          <a-input placeholder="请输入物流公司" v-model="model.expressCompany" autocomplete="on"/>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="expressNo" label="物流单号">
          <a-input placeholder="请输入物流单号" v-model="model.expressNo" />
        </a-form-model-item>
        
        <input type="hidden" v-model="model.id" />
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
  import { getAction, httpAction } from '@/api/manage'

  export default {
    name: 'SimExpressModal',
    data () {
      return {
        title: '填写物流',
        visible: false,
        model: {
          id: '',
          expressNo: '',
          expressCompany: ''
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          expressNo: [{ required: true, message: '请输入物流单号' }],
          expressCompany: [{ required: true, message: '请输入物流公司' }]
        },
        url: {
          queryById: '/sim/simOrder/queryById',
          editExpress: '/sim/simOrder/editExpress'
        }
      }
    },
    methods: {
      open(id) {
        this.title = '修改物流'
        this.visible = true
        this.model = { id: id, expressNo: '', expressCompany: '' }
        if (id) {
          getAction(this.url.queryById, { id }).then(res => {
            if (res && res.success && res.result) {
              this.model.expressNo = res.result.expressNo || '';
              this.model.expressCompany = res.result.expressCompany || '';
            }
          })
        }
      },
      handleOk () {
        this.$refs.form.validate(valid => {
          if (!valid) return
          this.confirmLoading = true
          httpAction(this.url.editExpress, this.model, 'put').then(res => {
            if (res && res.success) {
              this.$message.success(res.message || '修改成功')
              this.$emit('ok')
              // 仅成功时关闭弹窗
              this.handleCancel()
            } else {
              this.$message.warning((res && res.message) || '修改失败')
            }
          }).finally(() => {
            this.confirmLoading = false
          })
        })
      },
      handleCancel () {
        this.visible = false
        if (this.$refs.form) {
          this.$refs.form.clearValidate()
        }
      }
    }
  }
</script>

<style lang="less" scoped>

</style>


